﻿using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class LimitPricesUpdate : BasePage
    {
        IBLL_Goods ig = Factory.BLL_Goods();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind();
            }
        }

        public string GetWhere()
        {
            string where = string.Empty;
            if (!string.IsNullOrEmpty(txtGoodsId.Text.Trim()))
            {
                where += " and a.goodsid like '%" + txtGoodsId.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtGoodsName.Text.Trim()))
            {
                where += " and goodsname like '%" + txtGoodsName.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtOutLook.Text.Trim()))
            {
                where += " and outlookc like '%" + txtOutLook.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtMaterial.Text.Trim()))
            {
                where += " and Material like '%" + txtMaterial.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtUnit.Text.Trim()))
            {
                where += " and unit like '%" + txtUnit.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtPackUnit.Text.Trim()))
            {
                where += " and packunit like '%" + txtPackUnit.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtQualityLevel.Text.Trim()))
            {
                where += " and QualityLevel like '%" + txtQualityLevel.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtProduceCompany_SC.Text.Trim()))
            {
                where += " and CompanyName_SC_tb like '%" + txtProduceCompany_SC.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtpinyincode.Text.Trim()))
            {
                where += " and pinyincode like '%" + txtpinyincode.Text.Trim() + "%'";
            }
            if (ddlProjectid.SelectedIndex != -1)
            {
                where += " and Projectid = '" + ddlProjectid.SelectedValue+ "'";
            }
            if (ddlIsConfirm.SelectedIndex != 0)
            {
                where += " and nvl(isconfirm,0) = '" + ddlIsConfirm.SelectedValue + "'";
            }
            return where;
        }

        public void Bind()
        {
            int totalCount = 0;
            gvwGoodsList.DataSource = ig.GetLimitPriceByWhere(GetWhere(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwGoodsList.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //int totalCount = 0;
            //DataTable dt = ig.GetLimitPriceByWhere(GetWhere(), 1, 0, out totalCount);
            //HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(dt, "", "限价情况.xls");
            string sql = string.Format(@"SELECT '‘'||a.Goodsid 产品编号,GoodsName 产品名称,'‘'||Outlookc 规格型号,Material 材质,Unit 单位,
                                        PackUnit 包装单位,QualityLevel 产品类别,CompanyName_SC_tb 生产企业代理,ReportedPrice 企业自报价,ReferencePrice 参考价,ActualPrice 实际采购价格,LimitedPrice 限价 FROM TB_LimitedPrice a left join view_procurecatalog_tb b on a.goodsid = b.goodsid  WHERE 1=1 {0}", GetWhere());
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "限价情况.xls");
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtGoodsId.Text = "";
                //txtFactor.Text = "";
                txtQualityLevel.Text = "";
                txtPackUnit.Text = "";
                //txtPack.Text = "";
                txtProduceCompany_SC.Text = "";
                txtGoodsName.Text = "";
                txtUnit.Text = "";
            }
        }

        protected void gvwGoodsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwGoodsList.EditIndex = -1;
            Bind();
        }

        protected void gvwGoodsList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwGoodsList.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void gvwGoodsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtReferencePrice = (TextBox)gvwGoodsList.Rows[e.RowIndex].FindControl("txtReferencePrice");
            TextBox txtActualPrice = (TextBox)gvwGoodsList.Rows[e.RowIndex].FindControl("txtActualPrice");
            TextBox txtLimitedPrice = (TextBox)gvwGoodsList.Rows[e.RowIndex].FindControl("txtLimitedPrice");
            if (string.IsNullOrEmpty(txtReferencePrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("参考价不能为空！", this);
                txtReferencePrice.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtActualPrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("实际采购价不能为空！", this);
                txtActualPrice.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtLimitedPrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("限价不能为空！", this);
                txtLimitedPrice.Focus();
                return;
            }
            LimitedPrice lpmodel = ig.GetLimitedPriceByID(gvwGoodsList.DataKeys[e.RowIndex].Value.ToString());
            LimitedPrice lp = new LimitedPrice();
            lp.Goodsid = gvwGoodsList.DataKeys[e.RowIndex].Value.ToString();
            lp.ReferencePrice =Convert.ToDecimal(txtReferencePrice.Text.Trim());
            lp.ActualPrice = Convert.ToDecimal(txtActualPrice.Text.Trim());
            lp.LimitPrice = Convert.ToDecimal(txtLimitedPrice.Text.Trim());
            string logs = "操作人："+CurrentAccount.GetUser().UserName+"，操作时间："+DateTime.Now+"。";
            if (lpmodel.ReferencePrice != lp.ReferencePrice)
            {
                logs += "原参考价：" + lpmodel.ReferencePrice + "，现参考价：" + lp.ReferencePrice + "。";
            }
            if (lpmodel.ActualPrice != lp.ActualPrice)
            {
                logs += "原实际采购价：" + lpmodel.ActualPrice + "，现实际采购价：" + lp.ActualPrice + "。";
            }
            if (lpmodel.LimitPrice != lp.LimitPrice)
            {
                logs += "原限价：" + lpmodel.LimitPrice + "，现限价：" + lp.LimitPrice + "。";
            }
            if (!string.IsNullOrEmpty(logs))
            {
                lp.UpdateLog = lpmodel.UpdateLog+logs;
            }
            if (ig.GetUpdatelimitPrice(lp) == 1)
            {
                gvwGoodsList.EditIndex = -1;
                Bind();
            }
            else
            {
                PageError("修改失败！", "");
            }
        }
    }
}